package easy;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class RemoveDuplicatesFromSortedArray {
    //不符合题目要求，题目在原数组上操作
    public int removeDuplicates0(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        for(int i=0;i<nums.length;i++){
            set.add(nums[i]);
        }
        int[] numArray = new int[set.size()];
        int j = 0;
        for(Integer i:set){
            numArray[j++] = i;
        }
        for(int i=0;i<numArray.length;i++){
            System.out.println(numArray[i]);
        }
        nums = Arrays.copyOf(numArray,numArray.length);
        return nums.length;
    }

    public int removeDuplicates1(int[] nums) {
        int j = 0;
        for(int i=0;i<nums.length;i++){
            if(nums[i] == nums[i+1]){
                nums[j] = nums[i];
                j++;
                i++;
            }else{
                nums[j] = nums[i];
                j++;
                i++;
            }
        }
        return nums.length;
    }

    public int removeDuplicates(int[] nums) {
        int j = 0;
        for(int i=0;i<nums.length;i++){
            if(nums[j] != nums[i]){
                j++;
                nums[j] = nums[i];
            }
        }
        return j+1;
    }
}
